﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using easyList.Entities;

namespace easyList.IO.Writer
{
    class XMLWriter
    {
        static XMLWriter instance = null;
        static readonly object padlock = new object();


        XMLWriter() { }
        public static XMLWriter Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new XMLWriter();
                    }
                    return instance;
                }
            }
        }

        public void writeXML(ArchiveObject archive, string destinationpath)
        {
           
            XmlTextWriter textWriter = new XmlTextWriter(destinationpath, null);
            // Opens the document
            textWriter.Formatting = Formatting.Indented;
            textWriter.WriteStartDocument();
            // Write comments
            textWriter.WriteComment("easyList XML Format");
            
            textWriter.WriteComment(DateTime.Now.ToString());

            // Write first element
            textWriter.WriteStartElement("Archive");
            textWriter.WriteAttributeString("size", archive.contentList.Count.ToString() );
          
            foreach ( FileObject currentFile in archive.contentList){
                textWriter.WriteStartElement("File");

                     textWriter.WriteStartElement("Filename");
                     textWriter.WriteString(currentFile.filename);
                     textWriter.WriteEndElement();

                     textWriter.WriteStartElement("StoreID");
                     textWriter.WriteString(currentFile.storeID);
                     textWriter.WriteEndElement();

                     textWriter.WriteStartElement("Size");
                     textWriter.WriteString(currentFile.formattedSize);
                     textWriter.WriteEndElement();

                textWriter.WriteEndElement();
            }

   
            textWriter.WriteEndElement();

            // Ends the document.
            textWriter.WriteEndDocument();


            // close writer
            textWriter.Close();

        }



    }
}
